package puyu.manage.service.role.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import puyu.manage.service.SysUser.service.SysUserServiceI;
import puyu.manage.service.common.config.BaseController;
import puyu.manage.service.common.page.TableDataInfo;
import puyu.manage.service.constant.AjaxResult;
import puyu.manage.service.role.service.SysRoleServiceI;
import puyu.manage.service.utils.UserConstants;
import puyu.manage.service.utils.except.CustomException;

import java.util.List;
import java.util.Map;

import static puyu.manage.service.constant.AjaxResult.success;

/**
 *  @ClassName: 角色信息管理
 *  @author: whl
 *  @Date: 2024/6/6 15:26
 *  @Description: Description of this class
 */
@RestController
@Api(tags = "角色信息")
@RequestMapping("/system/role")
public class SysRoleController extends BaseController {

    @Autowired(required = false)
    private SysRoleServiceI roleService;



    @ApiOperation("获取角色列表")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Map<String,Object> role) {
        startPage();//需传pageNum pageSize orderByColumn（排序列 ） isAsc（正序倒序）
        List<Map<String,Object>> list = roleService.selectRoleList(role);
        return getDataTable(list);
    }


    @ApiOperation("通过角色添加用户")
    @PostMapping("/users")
    @Transactional(rollbackFor =Exception.class)
    public AjaxResult addUser(@RequestBody Map<String,Object> role) {
        return success(roleService.addUsers(role));
    }


    /**
     * 根据角色编号获取详细信息
     */
    @ApiOperation("根据角色编号获取详细信息")
    @GetMapping("{roleId}")
    public AjaxResult getInfo(@PathVariable String roleId) {
        Map<String, Object> stringObjectMap = roleService.selectRoleById(roleId);
        return success(stringObjectMap);
    }

    /**
     * 新增角色
     */
    @ApiOperation("新增角色")
    @PostMapping
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(@Validated @RequestBody Map<String,Object> role) {
        if ("0".equals(roleService.checkRoleNameUniqueInster(role))) {
            throw new CustomException("A0308", role.get("roleName"));
        } else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) {
            throw new CustomException("A0309", role.get("roleName"));
        }
        roleService.insertRole(role);

        return success("新增成功");
    }

    /**
     * 修改角色
     */
    @ApiOperation("修改角色")
    @PutMapping
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult update(@Validated @RequestBody Map<String,Object> role) {
        if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
            throw new CustomException("A0310", role.get("roleName"));
        } else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) {
            throw new CustomException("A0311", role.get("roleName"));
        }
        roleService.updateRole(role);
        return success();
    }

    /**
     * 根据角色ID查询已赋权菜单
     */
    @ApiOperation("根据角色ID查询已赋权菜单")
    @GetMapping("/getByRoleId")
    public AjaxResult getByRoleId(String roleId) {
        List<String> byRoleId = roleService.getByRoleId(roleId);
        return success(byRoleId);
    }


    /**
     * 角色菜单关联
     * @param roleMenu
     * @return
     */
    @ApiOperation("角色菜单关联")
    @PutMapping("/roleMenu")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult roleMenu(@RequestBody Map<String,Object> roleMenu) {
        roleService.roleMenu(roleMenu);
        return success("操作成功");
    }

    /**
     * 角色状态修改
     */
    @ApiOperation("角色状态修改")
    @PutMapping("/changeStatus")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult changeStatus(@RequestBody Map<String,Object> role) {
        return toAjax(roleService.updateRoleStatus(role));
    }

    /**
     * 删除角色
     */
    @ApiOperation("删除角色")
    @DeleteMapping("/deleteById")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult delete(@RequestParam(value = "roleIds") List<String> roleIds) {
        return toAjax(roleService.deleteRoleByIds(roleIds));
    }


    /**
     * 获取角色选择框列表
     */
    @ApiOperation("获取角色选择框列表")
    @GetMapping("/optionselect")
    public AjaxResult optionselect() {
        return success(roleService.selectRoleAll());
    }
}
